package com.itjiange.reglg.service.impl;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.itjiange.reglg.model.pojo.MyEmail;
import com.itjiange.reglg.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 发送邮件服务
 *
 * @author ItJiange
 * @date 2023/11/13
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private MyEmail myEmail;

    /**
     * 验证码提醒
     * @param confirmCode
     * @param receiveEmail
     */
    @Override
    public void sendVerCodeEmail(String confirmCode, String receiveEmail) {
        MailAccount account = configEmail();
        // 发送邮件
        MailUtil.send(account, receiveEmail, "验证码", "您的验证码为：" + confirmCode, false);
    }

    /**
     * 异地登录提醒
     * @param address
     * @param receiveEmail
     */
    @Override
    public void sendIPMsgEmail(String address, String receiveEmail) {
        MailAccount account = configEmail();
        MailUtil.send(account, receiveEmail, "异地登录提醒", "您本次登录的地址为：" + address + "如非本人登录，请修改密码", false);
    }

    private MailAccount configEmail() {
        MailAccount account = new MailAccount();
        // 邮件服务器的SMTP地址
        // 每个邮件服务器的SMTP地址不同，注意修改，这边以163为例
        account.setHost(myEmail.getHost());
        // 端口默认25
        account.setPort(myEmail.getPort());
        account.setAuth(true);
        // 发件人
        account.setFrom(myEmail.getFrom());
        // 用户名，默认为发件人邮箱前缀
        account.setUser(myEmail.getUser());
        // 密码(注意不是登陆密码，是网易客户端登陆受权码),
        account.setPass(myEmail.getPass());
        return account;
    }
}
